﻿#include "BrowserTableHandlerSubsystemCavity.h"
#include "OperatorsInterface/OperAbstractSubsystem.h"
#include "FITK_Component/FITKAcousticsModel/FITKAbstractSEASubSys.h"
#include "FITK_Component/FITKAcousticsModel/FITKSEASubSysCavity.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"

namespace GUI
{
    void BrowserTableHandlerSubsystemCavity::updateTable()
    {
        if (_tableWidget == nullptr) return;
        _tableWidget->clear();
        _tableWidget->setColumnCount(0);
        _tableWidget->setRowCount(0);

        //设置表头
        QStringList headerLabels;
        headerLabels << tr("Name") << tr("Fluid") << tr("Surface Area[m3]")
            << tr("Volume[m3]") << tr("Notes");
        _tableWidget->setColumnCount(headerLabels.size());
        _tableWidget->setHorizontalHeaderLabels(headerLabels);
        //更新表格
        this->updateData();
    }

    void BrowserTableHandlerSubsystemCavity::updateData()
    {
        if (_tableWidget == nullptr) return;
        if (_seaMgr == nullptr || _propMgr == nullptr)return;

        QList<Acoustics::FITKAbstractSEASubSys*> list = _seaMgr->getSubSysByType(
            Acoustics::FITKAbstractSEASubSys::SEACavity);
        //声腔数量
        const int n = list.size();
        _tableWidget->setRowCount(n);

        for (int i = 0; i < n; ++i)
        {
            //转换为SEA声腔
            Acoustics::FITKSEASubSysCavity* sea = dynamic_cast<Acoustics::FITKSEASubSysCavity*>(list.at(i));
            if (sea == nullptr) continue;

            QString materialName{};
            Acoustics::FITKAcousticsAbsMaterial* materialData = sea->getAcousticsMaterial();
            if (!materialData)continue;
            materialName = materialData->getDataObjectName();

            double area = sea->getSurfaceArea();
            double volume = sea->getVolume();

            //数据ID
            const int id = sea->getDataObjectID();
            _tableWidget->setItem(i, 0, new QTableWidgetItem(sea->getDataObjectName(), id));
            _tableWidget->setItem(i, 1, new QTableWidgetItem(materialName, id));
            _tableWidget->setItem(i, 2, new QTableWidgetItem(QString::number(area), id));
            _tableWidget->setItem(i, 3, new QTableWidgetItem(QString::number(volume), id));
            //
        }
        _tableWidget->autoResizeCellWidth();
        _tableWidget->setTableItemEditable(false);
    }

    EventOper::OperAbstractSubsystem * BrowserTableHandlerSubsystemCavity::getSubsystemOper()
    {
        //从操作器仓库获取
        return Core::FITKOperatorRepo::getInstance()->
            getOperatorT<EventOper::OperAbstractSubsystem>("OperSubsystemCavity");
    }

    bool BrowserTableHandlerSubsystemCavity::enableAttriSet()
    {
        QList<QTableWidgetItem*> selectItems = this->getSelectItems();
        if (selectItems.size() < 2) return false;

        QTableWidgetItem* item = selectItems.at(0);
        if (item == nullptr) return false;
        //名称列
        const int colIndex = item->column();
        //流体材料，表面积，体积
        if (colIndex<1|| colIndex >3 ) return false;

        return true;
    }
}